package sgi.client;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;

import sgi.details_new.ImageDetails;
import sgi.message.MessageSearch;
import sgi.misc.FloatDimension;

public class SGIWatchPurchasedImagesController extends SGIController{
	
	public static enum DataLayer {Commerce,Infrastracture};
	
	public static SGIWatchPurchasedImagesController watch;
	private static SGIWatchPurchasedImagesUI watchUI;
	
	public static SGIWatchPurchasedImagesController create() throws IOException
	{
		if(SGIWatchPurchasedImagesController.watch == null)
		{
			SGIWatchPurchasedImagesController.watch = new SGIWatchPurchasedImagesController();
			SGIWatchPurchasedImagesUI.setController(watch);
		}
		
		watchUI.setVisible(true);
		watch.getImageDetails();
		return watch;
	}
	
	
	private SGIWatchPurchasedImagesController()
	{
		watchUI = new SGIWatchPurchasedImagesUI();
	}
	
	
	public static void terminate()
	{
		SGIWatchPurchasedImagesController.watch = null;
		SGIWatchPurchasedImagesUI.setController(null);
		SGIWatchPurchasedImagesController.watchUI.setVisible(false);
		SGIWatchPurchasedImagesController.watchUI = null;
	}


	public void getImageDetails()
	{
		File folder = new File(System.getProperty("user.home")+"/My Documents/SGIimages."+SGIUserAccountMenuController.getUser().getUsername());
		File[] listOfFiles = folder.listFiles();
		if(listOfFiles != null)
		{
		    for (int i = 0; i < listOfFiles.length; i++) {
		      if (listOfFiles[i].isFile()) {
		    	  String[] tokens = listOfFiles[i].getName().split(Pattern.quote("%"));
		    	  if(tokens[0].equals("Image") || tokens[0].equals("Sample"))
		    	  		watchUI.addRow(new ImageDetails(tokens[0],Integer.valueOf(tokens[1]),tokens[2],tokens[3]));
		      }
		    }
		}
	}
	
	
	public void viewImage(ImageDetails details)
	{
		SGIImageViewer.viewer = new SGIImageViewer(details);
		SGIImageViewer.viewer.setVisible(true);
	}
	

	public void imageSequence()
	{
		/*Retrieve the list of images*/
		List<ImageIcon> ImageList = null;
		File folder = new File(System.getProperty("user.home")+"/My Documents/SGIimages."+SGIUserAccountMenuController.getUser().getUsername());
		File[] listOfFiles = folder.listFiles();
		if(listOfFiles != null)
		{
			ImageList = new ArrayList<ImageIcon>();
		    for (int i = 0; i < listOfFiles.length; i++) {
		      if (listOfFiles[i].isFile()) {
		    	  File f = new File(listOfFiles[i].getAbsolutePath());
		    	  if(f.exists())
		    		  ImageList.add(new ImageIcon(listOfFiles[i].getAbsolutePath()));
		      }
		    }
		    
		    SGIImageSequenceViewer.sequenceViewer = new SGIImageSequenceViewer(ImageList);
		    SGIImageSequenceViewer.sequenceViewer.setVisible(true);
		    
		}
	}
	
	@Override
	void handleMessage(Object msg) {
		// TODO Auto-generated method stub
		
	}

}
